package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * Description: 登陆页面
 * User: Li_yizYa
 * Date: 2025/9/20
 * Time: 13:53
 */
public class LoginPage extends Utils {

    public static String url = "http://101.42.27.228:8080/blogin.html";

    public LoginPage() {
        super(url);
    }

    /**
     * 检查页面是否可以正常打开
     */
    public void checkPageRight() {
        // 通过检查关键元素是否存在
        // 检查账号登录、邮箱登录是否加载成功
        driver.findElement(By.cssSelector(
                "body > div > div.login-container.col-sm-6.col-md-6.col-lg-5.col-xl-5 > div.tab-box > span:nth-child(1)"));
        driver.findElement(By.cssSelector(
                "body > div > div.login-container.col-sm-6.col-md-6.col-lg-5.col-xl-5 > div.tab-box > span.tab-span.active"));

        // 检查登录框是否加载成功
        driver.findElement(By.cssSelector("#phoneNumber"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#loginForm > button"));
    }

    /**
     * 异常登录
     * --- 用户名和密码都为空
     * --- 用户名为空，密码不为空
     * --- 用户名不为空，密码为空
     * --- 输入正确的账号，错误的密码
     * --- 输入错误的账号(不存在)
     */

    /**
     * 用户名和密码都为空
     */
    public void loginFail01() {
        driver.findElement(By.cssSelector("#loginForm > button")).click();

        // 判断提示是否符合预期
        String expectStr1 = "请输入您的手机号或邮箱号";
        String expectStr2 = "请输入密码";
        String curStr1 = driver.findElement(By.cssSelector("#phoneNumber-error")).getText();
        String curStr2 = driver.findElement(By.cssSelector("#password-error")).getText();
        assert expectStr1.equals(curStr1);
        assert expectStr2.equals(curStr2);
    }

    /**
     * 用户名为空，密码不为空
     */
    public void loginFail02() {
        // 先清空输入框
        driver.findElement(By.cssSelector("#phoneNumber")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        driver.findElement(By.cssSelector("#password")).sendKeys("20021117");
        driver.findElement(By.cssSelector("#loginForm > button")).click();

        // 判断提示是否符合预期
        String expectStr1 = "请输入您的手机号或邮箱号";
        String curStr1 = driver.findElement(By.cssSelector("#phoneNumber-error")).getText();
        assert expectStr1.equals(curStr1);
    }

    /**
     * 用户名不为空，密码为空
     */
    public void loginFail03() {
        // 先清空输入框
        driver.findElement(By.cssSelector("#phoneNumber")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("17612932553");
        driver.findElement(By.cssSelector("#loginForm > button")).click();

        // 判断提示是否符合预期
        String expectStr1 = "请输入密码";
        String curStr1 = driver.findElement(By.cssSelector("#password-error")).getText();
        assert expectStr1.equals(curStr1);
    }


    /**
     * 正确的账号，错误的密码
     */
    public void loginFail04() {
        // 先清空输入框
        driver.findElement(By.cssSelector("#phoneNumber")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        // 输入正确的账号，错误的密码
        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("17612932553");
        driver.findElement(By.cssSelector("#password")).sendKeys("2002lyz123");
        driver.findElement(By.cssSelector("#loginForm > button")).click();

        // 处理错误的弹窗 --- 警告弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();;
        String actualMsg = alert.getText();
        alert.accept();

        String expectMsg = "登陆失败: 密码错误";
        assert actualMsg.equals(expectMsg);
    }

    /**
     * 输入错误的账号(不存在)
     */
    public void loginFail05() {
        // 先清空输入框
        driver.findElement(By.cssSelector("#phoneNumber")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        // 输入正确的账号，错误的密码
        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("17612932559");
        driver.findElement(By.cssSelector("#password")).sendKeys("2002lyz123");
        driver.findElement(By.cssSelector("#loginForm > button")).click();

        // 处理错误的弹窗 --- 警告弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();;
        String actualMsg = alert.getText();
        alert.accept();

        String expectMsg = "登陆失败: 用户信息为空";
        assert actualMsg.equals(expectMsg);
    }

    /**
     * 输入正确的账号密码
     */
    public void loginSuccess() {
        // 先清空输入框
        driver.findElement(By.cssSelector("#phoneNumber")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        // 输入正确的账号，错误的密码
        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("17612932553");
        driver.findElement(By.cssSelector("#password")).sendKeys("2002lyz");
        driver.findElement(By.cssSelector("#loginForm > button")).click();

        // 登录成功会进入到后台中心页面
        driver.findElement(By.cssSelector("body > div.header-box > div.user-box"));
    }
}
